package com.aluvfy.jdbc;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest03 {
    public static void main(String[] args) {

        //读取属性配置文件，读取数据库连接
        ResourceBundle bundle = ResourceBundle.getBundle("com.aluvfy.jdbc.jdbc");
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");

        Connection conn = null;
        Statement stm = null;
        ResultSet rs = null;
        try {
            //1. 注册驱动
            Class.forName(driver);
            //2. 获取连接
            conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
            //3.获取数据库操作对象
            stm = conn.createStatement();
            //4. 执行sql语句
            String sql = "select realname, id, name, password from t_user";
            rs = stm.executeQuery(sql);
            //5. 处理查询结果集
            //这里的处理方式是直接打印
            /*
            * +----------+----+--------+----------+
            | realname | id | name   | password |
            +----------+----+--------+----------+
            | 千千     |  1 | admin  | admin123 |
            | 双神     |  2 | aluvfy | 123456   |
            | 王五     |  3 | wangwu | 12345    |
            +----------+----+--------+----------+
            * */
//            boolean hasNext = rs.next();  //nest 光标指向当前行 有数据 返回true  当没有数据的时候 返回false
//            if (hasNext) {
//                //取出光标当前行的数据，有很多种取出的方式
//                //不论数据库种是什么数据类型，都以String打印出
//                //以下的获取方式是通过获取列的方式  1 relname 2 id  3 name  4 passwold
//                String realname = rs.getString(1);
//                String id = rs.getString(2);
//                String name= rs.getString(3);
//                String psd = rs.getString(4);
//                System.out.println(realname + "\t" + id + "\t" + name + "\t" + psd);
//            }
            while (rs.next()){
                String realname = rs.getString(1);
                String id = rs.getString(2);
                String name= rs.getString(3);
                String psd = rs.getString(4);
                System.out.println(realname + "\t" + id + "\t" + name + "\t" + psd);
            }
            //查询语句二  t_employee
            String sql2 = "select id, name, salary from t_employee";
            rs = stm.executeQuery(sql2);
            while (rs.next()){
                int id = rs.getInt(1);
                String name= rs.getString(2);
                double salary = rs.getDouble(3);
                System.out.println(id + "\t" + name + "\t" + salary);
            }
            System.out.println("=================================");
            //查询语句三  t_employee 通过查询结果集种的列名
            String sql3 = "select id, name, salary from t_employee";
            rs = stm.executeQuery(sql3);
            while (rs.next()) {
                int id = rs.getInt("id");
                String name= rs.getString("name");
                double salary = rs.getDouble("salary");
                System.out.println(id + "\t" + name + "\t" + salary);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            //6. 释放资源
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
